package simplyrugby.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import simplyrugby.common.Result;
import simplyrugby.entity.Match;
import simplyrugby.service.MatchService;

import java.util.List;

@RestController
@RequestMapping("/api/match")
@RequiredArgsConstructor
public class MatchController {
    private final MatchService matchService;


    @PostMapping
    public Result add(@RequestBody Match match) {
        boolean save = matchService.save(match);
        return Result.success().data(save);
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable String id) {
        boolean b = matchService.removeById(id);
        return Result.success().data(b);
    }

    @PutMapping
    public Result updateById(@RequestBody Match match) {
        boolean flag = matchService.updateById(match);
        return flag ? Result.success() : Result.fail();
    }


    @GetMapping("/{id}")
    public Result details(@PathVariable String id) {
        Match match = matchService.details(id);
        return Result.success().data(match);
    }

    @GetMapping("/list")
    public Result list() {
        List<Match> list = matchService.list();
        return Result.success().data(list);
    }

    @PostMapping("/page")
    public Result page(@RequestParam(required = false) Integer pageNum,
                       @RequestParam(required = false) Integer pageSize,
                       @RequestBody Match match
    ) {
        Page<Match> page = matchService.list(pageNum, pageSize, match);
        return Result.success().data(page);
    }
}

